package com.cym.utils;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.Statements;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.drop.Drop;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Properties;

/**
 * Hello world!
 */
public class Mysql2Postgresql {

	public static void main(String[] args) throws JSQLParserException, IOException {
		String sqlContent = FileUtil.readString("d:\\source-mysql-ddl.sql", Charset.forName("utf-8"));
		sqlContent = sqlContent.replaceAll("`", "");

		StringBuilder totalSql = new StringBuilder();
		Statements statements = CCJSqlParserUtil.parseStatements(sqlContent);
		for (Statement statement : statements.getStatements()) {
			if (statement instanceof CreateTable) {
				String sql = ProcessSingleCreateTable.process((CreateTable) statement);
				totalSql.append(sql).append("\n");
			} else if (statement instanceof Drop) {
				String sql = ProcessSingleDropTable.process((Drop) statement);
				totalSql.append(sql).append("\n");
			} else {
				throw new UnsupportedOperationException();
			}
		}

		FileUtils.writeStringToFile(new File("d:\\source-postgresql-ddl.sql"), totalSql.toString(), StandardCharsets.UTF_8);
		System.out.println(totalSql);

	}

}
